package com.invengo.module_check.ui.fragment.opeartion;

import static com.invengo.module_check.data.entity.BundleKey.KEY_ACTION;
import static com.invengo.module_check.data.entity.BundleKey.KEY_BOOKS_GROUP;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELFS;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.invengo.base.excel.ExportExcelUtils;
import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentOpInfoBinding;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.http.interceptor.logging.LocalLogUtil;

public class OpeartionInfoFragment extends BaseFragment<FragmentOpInfoBinding, OpeartionInfoViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_op_info;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public String getRightTitle() {
        return "导出";
    }

    @Override
    public void onRightEvent(View view) {
        switch (actionArgs) {
            case ACTION_LISTING:
            case ACTION_REVOKE:
            case ACTION_INVENTORY:
            case ACTION_CHANGE:
            case ACTION_PARALLEL:
            case ACTION_FORCE:
                ExportExcelUtils.getInstance().exportBookBeans(getActivity(), bookBeans, getArguments().getString(BundleKey.KEY_TITLE));
                break;
            case ACTION_FIRST:
                ExportExcelUtils.getInstance().exportShelfBeans(getActivity(), shelfBeans, getArguments().getString(BundleKey.KEY_TITLE));
                break;
            case ACTION_DOWN:
                boolean isShelfResult = getArguments().getBoolean(BundleKey.KEY_IS_SHELF_RESULT, true);
                if (isShelfResult)
                    ExportExcelUtils.getInstance().exportShelfBeans(getActivity(), shelfwithBooks, getArguments().getString(BundleKey.KEY_TITLE));
                else
                    ExportExcelUtils.getInstance().exportBookBeans(getActivity(), bookBeans, getArguments().getString(BundleKey.KEY_TITLE));
                break;
        }
    }

    ActionArgs actionArgs;
    List<BookBean> bookBeans;
    Map<String, List<BookBean>> shelfwithBooks;
    List<ShelfBean> shelfBeans;


    @Override
    public void initData() {
        actionArgs = (ActionArgs) getArguments().getSerializable(KEY_ACTION);
        bookBeans = (List<BookBean>) getArguments().getSerializable(BundleKey.KEY_BOOKS);
        String currentShelfName = getArguments().getString(BundleKey.KEY_CURRENT_SHELF);
        shelfwithBooks = (Map<String, List<BookBean>>) getArguments().getSerializable(KEY_BOOKS_GROUP);
        shelfBeans = (List<ShelfBean>) getArguments().getSerializable(KEY_SHELFS);
        LocalLogUtil.temp2File("-----OpeartionInfoFragment----------");
        for (BookBean bookBean : bookBeans) {
            LocalLogUtil.temp2File(bookBean.getBookName()+"  "+bookBean.getBarCode()+"  "+ bookBean.getTip()+"  外借状态： "+ bookBean.getState());
        }
        switch (actionArgs) {
            case ACTION_LISTING:
                for (BookBean bookBean : bookBeans) {
                    bookBean.setTip("");
                }
                viewModel.updateBookBeans(bookBeans);
                break;
            case ACTION_DOWN:
                for (BookBean bookBean : bookBeans) {
                    bookBean.setTip("");
                }
                boolean isShelfResult = getArguments().getBoolean(BundleKey.KEY_IS_SHELF_RESULT, true);
                if (isShelfResult) {
                    viewModel.updateShelfBeans(shelfwithBooks);
                } else {
                    viewModel.updateBookBeans(bookBeans);
                }
                break;
            case ACTION_INVENTORY:
                viewModel.updateBookBeans(bookBeans);
                break;
            case ACTION_CHANGE:
                for (BookBean bookBean : bookBeans) {
                    bookBean.setTip("");
                }
                viewModel.showTitle(TextUtils.isEmpty(currentShelfName) ? "" : "目标层位：" + currentShelfName);
                viewModel.updateChange(bookBeans);
                break;
            case ACTION_PARALLEL:
                viewModel.updateBookBeans(bookBeans);
                break;
            case ACTION_FIRST:
                viewModel.showTitle("首书详情");
                viewModel.updateShelfBeans(shelfBeans);
                break;
            case ACTION_FORCE:
                for (BookBean bookBean : bookBeans) {
                    bookBean.setTip("");
                }
                viewModel.showTitle(TextUtils.isEmpty(currentShelfName) ? "" : "目标层位：" + currentShelfName);
                viewModel.updateBookBeans(bookBeans);
                break;
            case ACTION_REVOKE:
                for (BookBean bookBean : bookBeans) {
                    bookBean.setTip("");
                }
                viewModel.updateBookBeans(bookBeans);
                break;
        }
    }

    @Override
    public void initViewObservable() {
        viewModel.ui.tipEvent.observe(this, s -> binding.count.setText(Html.fromHtml(s)));
    }
}
